#include "formtoolboxwidget.h"
#include "ui_formtoolboxwidget.h"

#include <QDateEdit>

FormToolBoxWidget::FormToolBoxWidget(QWidget *parent) :
    QWidget(parent),m_dataFormat(""),IsFillData(false),
    ui(new Ui::FormToolBoxWidget)
{
    ui->setupUi(this);

    //init listwidget
    ui->listWidget->clear();
    ui->dateEdit->setVisible(false);
    ui->btn_ok->setVisible(false);
    ui->btn_cancel->setVisible(false);

    ui->dateEdit->setDateTime(QDateTime::currentDateTime());

    connect(ui->listWidget,&QListWidget::currentItemChanged,[=]
        (QListWidgetItem * current, QListWidgetItem * previous){
        if(current){
            current->setTextAlignment(Qt::AlignCenter);
            current->setSizeHint(QSize(current->sizeHint().width(),35));
        }
        if(previous){
            previous->setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
            previous->setSizeHint(QSize(previous->sizeHint().width(),20));
        }
    });
    connect(ui->btn_reLoad,&QToolButton::clicked,[=]{
//        ui->btn_reLoad->setVisible(false);
        emit sig_reloadButton();
    });

    connect(ui->btn_ok,&QToolButton::clicked,[=]{
//        if(ui->btn_reLoad->isHidden())
//            ui->btn_reLoad->setVisible(true);
        emit AddBtnClicked(ui->dateEdit->date().toString(m_dataFormat));
    });
    connect(ui->btn_cancel,&QToolButton::clicked,[=]{
        ui->dateEdit->setVisible(false);
        ui->btn_ok->setVisible(false);
        ui->btn_cancel->setVisible(false);
        ui->btn_reLoad->setVisible(true);
    });

    connect(ui->listWidget,&QListWidget::currentItemChanged,[=](QListWidgetItem * current, QListWidgetItem * previous){
        emit sig_currentItemChanged(current,previous);
    });
}


FormToolBoxWidget::~FormToolBoxWidget()
{
    delete ui;
}

void FormToolBoxWidget::setTitle(QString title, QString titleset)
{
    ui->lbl_title->setText(title);
    ui->lbl_titleSet->setText(titleset);
    if(title.isEmpty() && titleset.isEmpty()){
        ui->frame->setVisible(false);
    }
}

void FormToolBoxWidget::setDataFormat(QString dataformat)
{
    m_dataFormat = dataformat;
}

void FormToolBoxWidget::setCurrentItem(int index)
{
    if(ui->listWidget->item(index)){
        ui->listWidget->setCurrentItem(ui->listWidget->item(index));
//        emit sig_currentItemChanged(ui->listWidget->currentItem(),new QListWidgetItem());
//        ui->listWidget->setItemSelected(ui->listWidget->item(index),true);
    }
}

void FormToolBoxWidget::addItem(QString str)
{
    QListWidgetItem * item = new QListWidgetItem(str);
    ui->listWidget->addItem(item);
    item->setSizeHint(QSize(item->sizeHint().width(),20));

    IsFillData = true;
}

void FormToolBoxWidget::clear()
{
    ui->listWidget->clear();
}

QListWidget *FormToolBoxWidget::getListWidget()
{
    return ui->listWidget;
}
